package com.ly.quickdev.library.activity;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ly.quickdev.library.R;
import com.ly.quickdev.library.adapter.ListBaseAdapter;

/**
 * 显示一个列表的activity
 * 如果需要一个自定义布局文件，请保持自定义的布局文件中，分别有R.id.loading,R.id.list,R.id.empty这3个id就可以
 * 
 * @author 李洋
 * 
 * @param <T>
 */
public abstract class DevBaseListActivity<T> extends DevBaseActivity implements
		OnItemClickListener, OnRefreshListener2<ListView> {
	/** 加载的数据 */
	public List<T> mList;

	/** 当前页码 */
	public int pageNo;
	/** 数据适配器 */
	public ListBaseAdapter<T> mAdapter;

	public PullToRefreshListView mListView;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_list);

		if (mList == null || mList.isEmpty()) {
			pageNo = 0;
			loadData();
		} else {
			mAdapter = newAdapter();
			mListView.setAdapter(mAdapter);
			showList();
		}
	}

	@Override
	public void onSupportContentChanged() {
		// TODO Auto-generated method stub
		super.onSupportContentChanged();
		mListView = (PullToRefreshListView) findViewById(R.id.list);
		mListView.setOnRefreshListener(this);
		mListView.setOnItemClickListener(this);
		/** 此处可扩展，可以修改为能自定义刷新模式 */
		mListView.setMode(getRefreshMode());
	}

	public Mode getRefreshMode() {
		return Mode.BOTH;
	}

	@SuppressWarnings("unchecked")
	public T getItem(int position) {
		return (T) mListView.getRefreshableView().getAdapter()
				.getItem(position);
	}

	/**
	 * 显示列表
	 */
	public void showList() {
		mListView.setVisibility(View.VISIBLE);
	}

	public void setEmptyMessage(CharSequence msg) {
	}

	/**
	 * 返回一个新的adapter
	 * 
	 * @return
	 */
	public ListBaseAdapter<T> newAdapter() {
		return new ListBaseAdapter<T>(getActivity(), mList) {

			@Override
			public View initView(int position, View convertView,
					ViewGroup parent) {
				// TODO Auto-generated method stub
				return DevBaseListActivity.this.getView(position, convertView,
						parent);
			}

			@Override
			public int getViewTypeCount() {
				// TODO Auto-generated method stub
				return DevBaseListActivity.this.getViewTypeCount();
			}

			@Override
			public int getItemViewType(int position) {
				// TODO Auto-generated method stub
				return DevBaseListActivity.this.getItemViewType(position);
			}

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return DevBaseListActivity.this.getCount();
			}
		};
	}

	public int getCount() {
		return mList.size();
	}

	public int getViewTypeCount() {
		return 1;
	}

	public int getItemViewType(int position) {
		return 0;
	}

	/**
	 * 数据改变后设置adapter
	 */
	public void setListAdapter() {
		if (mAdapter == null) {
			mAdapter = newAdapter();
			mListView.setAdapter(mAdapter);
		} else {
			mAdapter.notifyDataSetChanged();
		}
		showList();
		mListView.onRefreshComplete();
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		pageNo = 0;
		loadData();
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		pageNo++;
		loadData();
	}

	/**
	 * 此方法返回的是{@link android.widget.BaseAdapter#getView(int, View, ViewGroup)}
	 * 
	 * @param position
	 * @param convertView
	 * @param parent
	 * @return
	 */
	public abstract View getView(int position, View convertView,
			ViewGroup parent);

	/**
	 * 加载数据的方法
	 */
	public abstract void loadData();

	/**
	 * 每一行点击事件
	 */
	@Override
	public abstract void onItemClick(AdapterView<?> parent, View view,
			int position, long id);
}
